package reco.frame.demo.adapter;

import reco.frame.tv.R;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Handler;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class GalleryAdapter extends BaseAdapter {
	private Context mContext;

	public GalleryAdapter(Context context) {
		mContext = context;
	}

	public int getCount() {
		return mps.length;
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ImageView image = new ImageView(mContext);
		image.setImageResource(R.drawable.ic_launcher);
		image.setAdjustViewBounds(true);
		image.setLayoutParams(new Gallery.LayoutParams(
				100, 100));
		image.setFocusable(true);
		image.setOnFocusChangeListener(new OnFocusChangeListener() {
			
			@Override
			public void onFocusChange(final View item, boolean focus) {
				if (focus) {
					new Handler().postDelayed(new Runnable() {

						@Override
						public void run() {
							scaleToLarge(item);
						}
					}, 130);

				} else {
					
					returnCover(item);
				}
				
			}
		});
		return image;
	}
	AnimatorSet animatorSet;
	private void scaleToLarge(View item) {

		if (!item.isFocused()) {
			return;
		}

		animatorSet = new AnimatorSet();
		float scale=1.3f;
		ObjectAnimator largeX = ObjectAnimator.ofFloat(item, "ScaleX", 1f, scale);
		ObjectAnimator largeY = ObjectAnimator.ofFloat(item, "ScaleY", 1f,
				scale);

		animatorSet.setDuration(120);
		animatorSet.play(largeX).with(largeY);
		animatorSet.start();
	}

	private void returnCover(View item) {
		if (animatorSet == null) {
			return;
		}
		if (animatorSet.isRunning()) {
			animatorSet.cancel();
		}
		ObjectAnimator oa = ObjectAnimator.ofFloat(item, "ScaleX", 1f);
		oa.setDuration(120);
		oa.start();
		ObjectAnimator oa2 = ObjectAnimator.ofFloat(item, "ScaleY", 1f);
		oa2.setDuration(120);
		oa2.start();
	}
	
	private Integer[] mps = {  
	        R.drawable.ic_launcher,  
	        R.drawable.ic_launcher,  
	        R.drawable.ic_launcher,  
	        R.drawable.ic_launcher,  
	        R.drawable.ic_launcher,  
	        R.drawable.ic_launcher,  
	        R.drawable.ic_launcher,  
	        R.drawable.ic_launcher,  
	        R.drawable.ic_launcher,  
	        R.drawable.ic_launcher,  
	        R.drawable.ic_launcher,  
	        R.drawable.ic_launcher,  
	        R.drawable.ic_launcher
	}; 
}
